<?php
/**
 * Created by JetBrains PhpStorm.
 * User: akrug
 * Date: 21.06.12
 * Time: 15:15
 */
abstract class UcsMvc_AbstractDbTable extends Zend_Db_Table_Abstract{

    public function init(){
        //identifiziert den namen des Promaerschluessels
        $this->_setupPrimaryKey();
    }

    /**
     * gibt tabellenliste aller nicht 'geloeschten' zeilen zurueck
     *
     * @return array
     */
    public function getTableList(){
        $select = $this->select()
                        ->where($this->_name.'_deleted = 0');
                        //->where($this->_name.'_hidden = 0');
        $result = $this->fetchAll($select)->toArray();

        return $result;
    }

    /**
     * @return array - one defined Category
     */
    public function getRow($id){
        if(!self::deleted($id)){
            $select = $this->select()->where( $this->_primary[1].' = '.$id);
            $result = $this->fetchAll($select)->toArray();
            return $result[0];
        }

        return false;
    }

    /**
     * @param $params
     *
     * @return mixed
     */
    public function addRow($params){
        $cols = $this->_getCols();

        //fuegt das erstelldatum hinzu, falls das Attribut vorhanden ist
        if(in_array($this->_name.'_created', $cols)){
            $params[$this->_name.'_created'] = date('Y-m-d H:i:s');
        }

        //entfernt attribute aus uebergebenem array, die kein datenbankattribute sind
        foreach($params as $pIndex => $p){
            if(!in_array($pIndex, $cols)){
                unset($params[$pIndex]);
            }
        }

        return $this->insert($params);
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose sets deleted flag to 1
     */
    public function deleteRow($id){

        if(!self::deleted($id)){
            $rowsDeleted = $this->update(array($this->_name.'_deleted' => 1),  $this->_primary[1].' = '.$id);

            if(0 == $rowsDeleted){
                return false;
            }
        }

        return true;
    }

    /**
     * @param $params
     * @return bool
     *
     *  compares each given param index with columnnames
     *  if there is no match param will be unset
     */
    public function editRow($params, $id = null){
        $cols    = $this->_getCols();

        if(isset($params['id'])){
            $id = $params['id'];
        }

        if(!$this->deleted($id)){
            foreach($params as $pIndex => $p){
                if(!in_array($pIndex, $cols)){
                    unset($params[$pIndex]);
                }
            }
            $this->update($params,  $this->_primary[1].' = '.$id);

            return true;
        }

        return false;
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose locks if locked - unlocks if unlocked
     *
     */
    public function lock($id){

        if(!self::deleted($id)){
            $hidden = 1;

            if($this->locked($id)){
                $hidden = 0;
            }

            $rowsDeleted = $this->update(array($this->_name.'_hidden' => $hidden),  $this->_primary[1].' = '.$id);

            if(0 == $rowsDeleted){
                return false;
            }
        }

        return true;
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose checks if Category is locked
     */
    public function locked($id){
        if(!$id){
            return true;
        }

        $company = $this->getRow($id);

        if(isset($company[$this->_name.'_hidden'])){
            return (bool) $company[$this->_name.'_hidden'];
        }else{
            return false;
        }
    }

    /**
     * @param $id
     * @return bool - returns true if 'deleted'  = 0
     *              - returns false if 'deleted' = 1 OR id not found
     */
    public function deleted($id){
        if(!$id){
            return true;
        }

        $select = $this->select()
                        ->where( $this->_primary[1].' = '.$id);

        $result = $this->fetchAll($select)->toArray();

        if(empty($result)){
            return true;
        }elseif(isset($result[0][$this->_name.'_deleted'])){
            if($result[0][$this->_name.'_deleted'] == '1'){
                return true;
            }
        }

        return false;
    }

    /**
     * prueft ob eine Kategorie fuer die ausgabe zugelassen ist
     *
     * @param $id
     *
     * @return bool
     */
    public function isValid($id){
        if(!$this->deleted($id) && !$this->locked($id)){
            return true;
        }

        return false;
    }

}